var apiJs = require('../../utils/util.js')
var appData = getApp().globalData;
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrList: null,
    Logo:null,
    Sex:0,
    Birthday:null,
    nowTime: apiJs.formatTime(new Date()),
    Array: ['女','男'],
  },
  
  bindData:function(){
    var that=this;
    wx.request({
      url: appData.apiurl + "mobile/api/MemberCenter/Personal",
      method: "GET",
      data: { userId: wx.getStorageSync('UserInfo').UserID, custId: appData.custID },
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        wx.hideLoading()
        if (res.statusCode == 200 && res.data.code == 1200) {
          that.setData({
            arrList: res.data.content,
            Logo: res.data.content.HeadImg,
            Sex: res.data.content.UserinfoModel.userinfoApiDto.Sex,
            Birthday: res.data.content.UserinfoModel.userinfoApiDto.Birthday
          });
        }
      },
      fail: function (res) {
        wx.hideLoading()
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit(function () {
    });
    var that = this;
    wx.showLoading({ title: '加载中...', mask: true });
    that.bindData();
  },

  /**
   * 点击上传头像
   */
  chooseImageTap:function(event){
    let that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },

  chooseWxImage: function (type) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res);
        that.setData({
           Logo: res.tempFilePaths[0],
        })
         that.upload_file('http://img4test.winmobi.cn/Baidu/Upload', res.tempFilePaths[0],'jpg',
         function(res){
            //调用上传文件成功
           that.SaveSubmit({ HeadIMg: '', UserID: wx.getStorageSync('UserInfo').UserID, CustID: appData.custID })
         },
         function(res){
            //调用上传文件失败
         })
      }
    })
  },
  
  //上传文件
  upload_file: function (url, filePath,name,success, fail){
    console.log('a=' + filePath)
    wx.uploadFile({
      url: url,
      filePath: filePath ,
      name: name,
      // HTTP 请求中其他额外的参数
      formData: { 'customerid': appData.custID, 
      'userid': wx.getStorageSync('UserInfo').UserID,
      'imageType':0,//上传图片类型
      'imageSize':20,//返回图片大小
      'isWeixin':0},
      header: {
        'content-type': 'multipart/form-data'
      }, // 设置请求的 header
      success: function (res) {
       // var aaa = JSON.parse(res.data)
        //console.log(aaa.url);
        if (res.statusCode == 200 && !res.data.result_code) {
          typeof success == "function" && success(res.data);
        } else {
          typeof fail == "function" && fail(res);
        }
      },
      fail: function (res) {
        console.log(res);
        typeof fail == "function" && fail(res);
      }
    })
  },
 
/**
 * 绑定性别选择事件
 */
  bindPickerChange:function (event){
     var that=this;
     var val = event.detail.value;
     that.setData({
       Sex: val
     })
     that.SaveSubmit({ Sex: val, UserID: wx.getStorageSync('UserInfo').UserID, CustID: appData.custID })
  },
  /**
   * 绑定日期选择事件
   */
  bindDateChange: function (event) {
    var that = this;
    var val = event.detail.value.replace('-', '/').replace('-', '/');
    that.setData({
      Birthday: val
    })
    that.SaveSubmit({ Birthday: val, UserID: wx.getStorageSync('UserInfo').UserID, CustID: appData.custID})
  },

  /**
   * 提交个人信息修改
   * param: 参数 { "text":11, "name": 22}
   */
  SaveSubmit: function(param){
      wx.showLoading({ title: '提交中...', mask: true });
      wx.request({
        url: appData.apiurl+"member/api/MemberCenter/Update",
        method: "POST",
        data:JSON.stringify(param),
        header: {
          "content-type": "application/json",
          'X_ManuName': appData.manuName
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.code == 1200) {
            wx.hideLoading()
            wx.showToast({
              title: '提交成功',
              icon:'success',
              duration:2000
            })
            console.log(res.data)
          }
        },
        fail: function (res) {
          wx.hideLoading()
          console.log(res.data)
        },
        complete: function (res) {
          console.log(res.data)
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      var that=this
      console.log('下拉刷新')
      that.bindData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})